//
//  CCVSExtendedImageView.m
//  ClinicalCollections
//
//  Created by Pasirstein, David B on 4/15/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "CCVSExtendedImageView.h"
#import "CCVSImage+Utilities.h"

@interface CCVSExtendedImageView ()
@property (nonatomic, weak) UILabel * label; //for future use
@property (nonatomic, weak) UIViewController * vc;
@end

@implementation CCVSExtendedImageView

@synthesize label = _label; //for future use
@synthesize viewWidth = _viewWidth;
@synthesize largeImageURL = _largeImageURL;

@synthesize data = _data;
@synthesize iview = _iview;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//initialize the object...primary way it will be init'ed
-(id)initWithData:(id<CCVSCustomDataType>)data withFrame:(CGRect)frame viewController:(UIViewController*)vc
{
    self = [self initWithFrame:frame];
    self.vc = vc;
    self.data = data;  //this will be a CCVSImage
    return self;
}

-(NSURL *)largeImageURL
{
    if (!_largeImageURL) _largeImageURL = ((CCVSImage<CCVSCustomDataType>*)self.data).largeImageURL;
    return _largeImageURL;
}

-(UIImageView *)iview
{
    if (!_iview) _iview = self;
    return _iview;
}

-(BOOL)isExpandable //will return if the view should support dyanamic resizing
{
    return NO;
}

//should return the frame size with width and height populated
+(CGSize)calcViewHeight:(CGFloat)viewWidth fromObject:(id<CCVSCustomDataType>)data
{
    CGSize size = CGSizeMake(viewWidth, viewWidth * WIDTH_TO_HEIGHT_PROPORTION);
    return size;
}



//should build view to be displayed (would likely return self)
-(UIView*)buildView
{
    self.userInteractionEnabled = YES;
    //add the gesture recognizer
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self.vc action:@selector(handleTapGestureForImageViewType:)];
    tap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tap];
    ///
    CCVSImage<CCVSCustomDataType> *ci = (CCVSImage<CCVSCustomDataType>*)self.data;
    //we will load the image into it
    [CCVSImage asyncLoadThumbImageIntoView:ci intoCCVSExtendedImageView:self withSpinner:YES];

    return self;
}

-(BOOL)willSegueOnEvent //true/false if the view will require a segue
{
    return NO;  //no for now, b/c the receiving VC is not yet implemented
//    return YES;
}

//if true above this should return the name of the segue
//if this was requested as a result of a gesture event
//the gesture will be provided as well
-(NSString*)nameOfSegue:(UIGestureRecognizer*)gesture
{
    return nil;  //TODO name the segue
}


-(void)setViewWidth:(CGFloat)viewWidth
{
    _viewWidth = viewWidth;
    //now resize the frame given the width
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
                        viewWidth, [CCVSExtendedImageView calcViewHeight:viewWidth fromObject:self.data].height);
}

-(CGFloat)viewWidth
{
    if (_viewWidth != self.frame.size.width) {
        _viewWidth = self.frame.size.width;
    }
    return _viewWidth;
}


/*
-(void)setTextLabel:(NSString*)text
{
 CGSize labelSize = use text to determine needed size of the label limited by view width
 if (!label) {
   UILabel * l1 = create label (lightGray partially transparent background)
   l1.set any additional properties
   [self.view addsubview:l1]
   self.label = l1;
 } else {
   //we need to resize the existing label
   label.frame = CGRectMake(....
 }
 label.text = text;
}
*/

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
